/******************************************************************************* 
  Copyright (C), 2018-2050, auditoryworks Tech. Co., Ltd.
  Description:   MEDIA VIDEO模块  
  Function List:     
  History:
    <wangmc -- 2019-12-02>  创建
*******************************************************************************/ 

/*=============================================================================
                                 Includes                                  
 =============================================================================*/
#include "mediaPriv.h"
#include "hid.h"
#include "ConfigApi.h"


/*=============================================================================
                               Macro Definition
 =============================================================================*/
//#define HidAudioTest

/*=============================================================================
                        Local Structures and Typedef 
 =============================================================================*/

/*=============================================================================
                             Extern Variables                                  
=============================================================================*/
extern "C"  int MEDIA_wifiSignal(void);
extern "C"  int MEDIA_wifiLinkStatus(void);
extern "C"  int MEDIA_productWIfiThd(void);
extern "C"  int MEDIA_kvmTest(void);
extern "C"  int MEDIA_setDhcp(int);


extern "C"  int MEDIA_TailMatchLed();

extern "C"  int MEDIA_TailMatchingLed();

extern "C" int  MEDIA_GetStaMode(int *mode){
    Json::Value table;
     
    
    if( ConfigApi::IConfigInterface::GetInstance()->Get("StaMode", table)){
        *mode = (int)table["Status"].asBool();
        return 0;
    }else{
        return -1;
    }
    
}

extern "C" int  MEDIA_SetStaMode(int mode){
    Json::Value table;
	table["Status"] = (mode == 1 ? true : false);
	if(ConfigApi::IConfigInterface::GetInstance()->Set("StaMode", table, ConfigApi::ConfigOptions::ConfigOptionPart)){
        return 0;
    }else{
        return -1;
    }
}


/*=============================================================================
                             Local Variables                                  
 =============================================================================*/

/*=============================================================================
                             Function Definition                                  
 =============================================================================*/
static int MEDIA_HidEnableZoom(int enable)
{
    int ret;
    char key[8] = {0};
    int screenMode;
    VideoEncoder *pVideoEncoder;

    pVideoEncoder = mediaCtl_g.pVideoEncoder[USBTYPEC_OUT];
    
    sprintf(key, "%d", enable);
    ret = INI_KeySet(CONFIG_PATH, "alg", "aimode", key);
    if (ret)
    {
        return -1;
    }

    pthread_mutex_lock(&mediaCtl_g.algMutex);

    mediaCtl_g.cfg.aiMode = enable;
    
    if (0 == enable)
    {
        screenMode = 0;
    }
    else
    {
        if (mediaCtl_g.cfg.speakTrackMode == 2)
        {
            screenMode = 1;
        }
        else
        {
            screenMode = 0;
        }
    }

    pVideoEncoder->setScreenMode(pVideoEncoder, screenMode);

    pthread_mutex_unlock(&mediaCtl_g.algMutex);
    
    return 0;
}

static int MEDIA_HidGetZoom(void)
{    
    return mediaCtl_g.cfg.aiMode;
}

static int MEDIA_HidSetSpeakerTrack(int mode)
{
    int ret;
    char key[8] = {0};
    int screenMode;
    VideoEncCropCfg encCropCfg;
    VideoEncoder *pVideoEncoder;

    pVideoEncoder = mediaCtl_g.pVideoEncoder[USBTYPEC_OUT];
    
    sprintf(key, "%d", mode);
    ret = INI_KeySet(CONFIG_PATH, "alg", "speakertrack", key);
    if (ret)
    {
        return -1;
    }

    pthread_mutex_lock(&mediaCtl_g.algMutex);

    mediaCtl_g.cfg.speakTrackMode = mode;

    if (2 == mode)
    {
        encCropCfg.enable = 0;
        encCropCfg.rect.x = 0;
        encCropCfg.rect.y = 0;
        encCropCfg.rect.w = VI_WIDTH;
        encCropCfg.rect.h = VI_HEIGHT;
        pVideoEncoder->setEncodeCrop(pVideoEncoder, &encCropCfg);
            
        screenMode = 1;
    }
    else
    {
        screenMode = 0;
    }
    pVideoEncoder->setScreenMode(pVideoEncoder, screenMode);

    pthread_mutex_unlock(&mediaCtl_g.algMutex);
    
    return 0;
}

static int MEDIA_HidGetSpeakerTrack(void)
{    
    return mediaCtl_g.cfg.speakTrackMode;
}

static int MEDIA_HidSetOptions(VideoInOptions *pOptions)
{
    int status;
    VideoInput *pVideoInput;

    pVideoInput = mediaCtl_g.pVideoInput;
    if (!pVideoInput)
    {
        LOG_PrintError("pVideoInput is NULL!\n");
        return -1;
    }

    status = pVideoInput->setOptions(pVideoInput, pOptions);
    if (status)
    {
        LOG_PrintError("Fail to get options.\n");
        return -1;
    }
    
    return 0;
}

static int MEDIA_HidEnableMirror(int enable)
{
    int ret;
    char key[8] = {0};
    VideoInOptions options;

    memcpy(&options, &mediaCtl_g.cfg.inOptions, sizeof(VideoInOptions));

    options.mirror = enable;

    ret = MEDIA_HidSetOptions(&options);
    if (ret)
    {
        return -1;
    }

    mediaCtl_g.cfg.inOptions.mirror = options.mirror;

    sprintf(key, "%d", options.mirror);
    ret = INI_KeySet(CONFIG_PATH, "videoView", "mirror", key);
    if (ret)
    {
        return -1;
    }

    return 0;
}

static int MEDIA_HidGetMirror(void)
{    
    return mediaCtl_g.cfg.inOptions.mirror;
}

static int MEDIA_HidSetZoomTime(int times)
{
    int ret;
    char key[8] = {0};

    mediaCtl_g.cfg.zoomTimes = times;

    sprintf(key, "%d", mediaCtl_g.cfg.zoomTimes);
    ret = INI_KeySet(CONFIG_PATH, "alg", "tracktime", key);
    if (ret)
    {
        return -1;
    }

    return 0;
}

static int MEDIA_HidGetZoomTime(void)
{    
    return mediaCtl_g.cfg.zoomTimes;
}

static int MEDIA_HidSetSplitCnt(int cnt)
{
    int ret;
    char key[8] = {0};

    mediaCtl_g.cfg.maxSplitCnt = cnt;

    sprintf(key, "%d", mediaCtl_g.cfg.maxSplitCnt);
    ret = INI_KeySet(CONFIG_PATH, "alg", "splitcnt", key);
    if (ret)
    {
        return -1;
    }

    return 0;
}

static int MEDIA_HidGetSplitCnt(void)
{    
    return mediaCtl_g.cfg.maxSplitCnt;
}

static int MEDIA_HidGetMcuVer(char *pVer)
{    
    //AWI_NET_WORK_PROXY_DEV_INFO_S proxyInfo = {0};
    
	//awiNetWork_GetProxyInfo(&proxyInfo);
	//strcpy(pVer, proxyInfo.acSoftVer);

	return 0;
}


static int MEDIA_HidGetBodyCount(void)
{
    return mediaCtl_g.algCtl.bodyCount;
}

static int MEDIA_HidAudioStart(int chn)
{
#ifdef HidAudioTest
    int status;
    AudioEncoder *pAudioEncoder = mediaCtl_g.pAudioEncoder[chn];

    status = pAudioEncoder->start(pAudioEncoder);
    if (status)
    {
        LOG_PrintInfo("Fail to start encoder!\n");
        return -1;
    }
#endif    
    return 0;
}

static int MEDIA_HidAudioStop(int chn)
{
#ifdef HidAudioTest

    int status;
    AudioEncoder *pAudioEncoder = mediaCtl_g.pAudioEncoder[chn];

    status = pAudioEncoder->stop(pAudioEncoder);
    if (status)
    {
        LOG_PrintInfo("Fail to start encoder!\n");
        return -1;
    }
#endif     
    return 0;
}

static int MEDIA_HidAudioGet(int chn, HID_audioPacket_t *pPacket)
{
#ifdef HidAudioTest

    int status;
    AudioEncPacket aencPacket;
    AudioEncoder *pAudioEncoder = mediaCtl_g.pAudioEncoder[chn];

    status = pAudioEncoder->getPacket(pAudioEncoder, &aencPacket);
    if (status)
    {
        usleep(1000);
        return -1;
    }

    pPacket->pBuf = aencPacket.buffer;
    pPacket->dataLen = aencPacket.length;
#endif     
    return 0;
}

static int MEDIA_HidAudioRelease(int chn)
{
#ifdef HidAudioTest

    int status;
    AudioEncoder *pAudioEncoder = mediaCtl_g.pAudioEncoder[chn];

    status = pAudioEncoder->releasePacket(pAudioEncoder);
    if (status)
    {
        LOG_PrintInfo("Fail to start encoder!\n");
        return -1;
    }
#endif     
    return 0;
}

static int MEDIA_HidEnableUac(int state)
{
    mediaCtl_g.uacState = 1;
    
    if (state)
    {
        mediaCtl_g.isUacStop = 0;
    }
    else
    {
        mediaCtl_g.isUacStop = 1;
    }

    while (mediaCtl_g.uacState)
    {
        usleep(1000);
    }
    
    return 0;
}

static int MEDIA_HidEnableAudioIn(int enable)
{
    int ret;
    char key[8] = {0};
    
    mediaCtl_g.cfg.audioInState = enable;
    
    sprintf(key, "%d", enable);
    ret = INI_KeySet(CONFIG_PATH, "audio", "audioenable", key);
    if (ret)
    {
        return -1;
    }

    mediaCtl_g.isReboot = 1;
    
    return 0;
}

static int MEDIA_HidGetAudioIn(void)
{    
    return mediaCtl_g.cfg.audioInState;
}

static int MEDIA_HidCorrStart(int vpt)
{
    mediaCtl_g.corrVpt = vpt;
    mediaCtl_g.isCorr = 2;
    
    return 0;
}

static int MEDIA_HidCorrStop(HID_corrResult_t *pResult)
{    
    mediaCtl_g.corrCalc = 1;
    
    while (1)
    {
        if (mediaCtl_g.corrCalc == 0)
        {
            break;
        }

        usleep(10*1000);
    }

    pResult->pResult = mediaCtl_g.corrResult;
    pResult->resultNum = 8;
    
    return 0;
}

static int MEDIA_HidEnableFlip(int enable)
{
    int ret;
    char key[8] = {0};
    VideoInOptions options;

    memcpy(&options, &mediaCtl_g.cfg.inOptions, sizeof(VideoInOptions));

    options.flip = enable;

    ret = MEDIA_HidSetOptions(&options);
    if (ret)
    {
        return -1;
    }

    mediaCtl_g.cfg.inOptions.flip = options.flip;

    sprintf(key, "%d", options.flip);
    ret = INI_KeySet(CONFIG_PATH, "videoView", "flip", key);
    if (ret)
    {
        return -1;
    }

    return 0;
}

static int MEDIA_HidGetFlip(void)
{    
    return mediaCtl_g.cfg.inOptions.flip;
}

static int MEDIA_CommonGetPtz(int pan, int tilt, int zoom)
{    
    return 0;
}
static int MEDIA_CommonSetPtz(int *pan, int *tilt, int *zoom)
{    
    return 0;
}



int MEDIA_HidInit(void)
{
    HID_initParam_t initParam;

    initParam.enableZoom = MEDIA_HidEnableZoom;
    initParam.getZoom = MEDIA_HidGetZoom;
    initParam.getBodyCount = MEDIA_HidGetBodyCount;
    initParam.audioStart = MEDIA_HidAudioStart;
    initParam.audioStop = MEDIA_HidAudioStop;
    initParam.audioGet = MEDIA_HidAudioGet;
    initParam.audioRelease = MEDIA_HidAudioRelease;
    initParam.enableUac = MEDIA_HidEnableUac;
    initParam.enableAudioIn = MEDIA_HidEnableAudioIn;
    initParam.getAudioIn = MEDIA_HidGetAudioIn;
    initParam.corrStart = MEDIA_HidCorrStart;
    initParam.corrStop = MEDIA_HidCorrStop;
    initParam.setSpeakerTrack = MEDIA_HidSetSpeakerTrack;
    initParam.getSpeakerTrack = MEDIA_HidGetSpeakerTrack;
    initParam.enableMirror = MEDIA_HidEnableMirror;
    initParam.getMirror = MEDIA_HidGetMirror;
    initParam.setZoomTime = MEDIA_HidSetZoomTime;
    initParam.getZoomTime = MEDIA_HidGetZoomTime;
    initParam.setSplitCnt = MEDIA_HidSetSplitCnt;
    initParam.getSplitCnt = MEDIA_HidGetSplitCnt;
    initParam.getMcuVer = MEDIA_HidGetMcuVer;
    initParam.setOsdMirror = MEDIA_CommonSetOsdMirror;
    initParam.getOsdMirror = MEDIA_CommonGetOsdMirror;
    initParam.enableFlip = MEDIA_HidEnableFlip;
    initParam.getFlip = MEDIA_HidGetFlip;
    initParam.enablePrivacy = MEDIA_CommonEnablePrivacy;
    initParam.getPrivacy = MEDIA_CommonGetPrivacy;
    initParam.getPtz = MEDIA_CommonGetPtz;
    initParam.setPtz = MEDIA_CommonSetPtz;
    initParam.startWifi = MEDIA_productWIfiThd;
    initParam.getWifiStatus = MEDIA_wifiLinkStatus;
    initParam.getWifiSignalStatus = MEDIA_wifiSignal;
    initParam.kvmTest = MEDIA_kvmTest;
    initParam.setDhcp = MEDIA_setDhcp;
    initParam.matchingLed = MEDIA_TailMatchingLed;
    initParam.matchedLed = MEDIA_TailMatchLed;
    initParam.getStaMode = MEDIA_GetStaMode;
    initParam.setStaMode = MEDIA_SetStaMode;
    HID_Init(&initParam);
    return 0;
}

